<template>
  <div>
    <el-header>
      <span>收信箱</span>
      <el-button @click="refresh">刷新消息</el-button>
    </el-header>
    <el-table
      :data="tableData.filter(data => !search||data.timestamp.includes(search) ||data.filename.includes(search.toLowerCase())||data.access.includes(search.toLowerCase())||data.state.includes(search.toLowerCase()))"
      :default-sort="{prop:tableData.timestamp,order:'descending'}"
      style="width: 100%"
    >
      <el-table-column label="Date" prop="timestamp" sortable></el-table-column>
      <el-table-column label="Requester" prop="requester" sortable></el-table-column>
      <el-table-column label="File" prop="filename" vsortable></el-table-column>
      <el-table-column label="Access" prop="access" sortable></el-table-column>
      <el-table-column label="Validity" prop="validity" sortable></el-table-column>
      <el-table-column label="State" prop="state" sortable></el-table-column>
      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="accept(scope.$index, scope.row)">接受</el-button>
          <el-button size="mini" type="danger" @click="refuse(scope.$index, scope.row)">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
const NodeRSA = require("node-rsa");
const crypto = require("crypto");
const BASE64 = "base64";
export default {
  data() {
    return {
      requestType: {
        type: "receiver"
      },
      opInfo: {
        state: "refuse",
        messageID: "1",
        random: "",
        signedData: ""
      },
      tableData: [],
      search: ""
    };
  },
  mounted: function() {
    this.refresh();
  },
  methods: {
    accept(index, row) {
      console.log(row)
      this.opInfo.state = "accept";
      this.opInfo.messageID = row.messageID;
      // 生成随机数并对随机数签名
      var buf = crypto.randomBytes(16);
      var random = buf.toString("hex");
      this.opInfo.random = random;
      
      var privateKey = new NodeRSA({ b: 512 });
      privateKey.importKey(window.localStorage.getItem("privateKey"));
      var signedData = privateKey
        .sign(Buffer.from(this.opInfo.random), BASE64)
        .toString(BASE64);
      this.opInfo.signedData = signedData;

      // console.log(this.opInfo);
      this.$axios.post("/home/operation", this.opInfo).then(res => {
        if (res.data.type === "success") {
          this.$message({
            message: res.data.content,
            type: res.data.type
          });
          this.refresh();
        } else {
          this.$message({
            message: res.data.content,
            type: res.data.type
          });
        }
      });
    },
    refuse(index, row) {
      this.opInfo.state = "refuse";
      this.opInfo.messageID = row.messageID;

      // 生成随机数并对随机数签名
      var buf = crypto.randomBytes(16);
      var random = buf.toString("hex");
      this.opInfo.random = random;

      var privateKey = new NodeRSA({ b: 512 });
      privateKey.importKey(window.localStorage.getItem("privateKey"));
      var signedData = privateKey
        .sign(Buffer.from(this.opInfo.random), BASE64)
        .toString(BASE64);
      this.opInfo.signedData = signedData;

      // console.log(this.opInfo);
      this.$axios.post("/home/operation", this.opInfo).then(res => {
        if (res.data.type === "success") {
          this.refresh();
          this.$message({
            message: res.data.content,
            type: res.data.type
          });
        } else {
          this.$message({
            message: res.data.content,
            type: res.data.type
          });
        }
      });
    },
    refresh() {
      this.$axios.post("/home/getMessage", this.requestType).then(res => {
        if (res.data.type === "success") {
          this.tableData = res.data.content;
        } else {
          this.$message({
            message: res.data.content,
            type: res.data.type
          });
        }
      });
    }
  }
};
</script>
<style scoped>
.el-header {
  background-color: #6d7981;
  display: flex;
  justify-content: space-between;
  color: #ffffff;
  font-size: 20px;
  align-items: center;
}
</style>